<template>
	<!-- 创建项目 -->
	<view class="container container-bg">
		<u-form :model="form" :border-bottom="false" ref="uForm" label-width="auto">
			<u-form-item label-width="160" required="true" label="负责人姓名" prop="pm_name">
				<u-input v-model="form.pm_name" disabled="true"/>
			</u-form-item>
			<u-form-item label-width="160" required="true" label="负责人职务" prop="pm_post">
				<u-input v-model="form.pm_post" disabled="true"/>
			</u-form-item>
			<u-form-item label-width="160" required="true" label="联系方式" prop="pm_phone">
				<u-input v-model="form.pm_phone" disabled="true"/>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="项目名称" prop="project_name">
				<u-input v-model="form.project_name" placeholder="请输入项目名称" />
				
			</u-form-item>
			<!-- 该地址使用自动定位 / 填写-->
			<u-form-item label-width="120" required="true" label="项目地址" prop="project_address">
				<u-input v-model="form.project_address" placeholder="请选择项目地址" type="select" :select-open="regionShow" @click="regionShow = true"/>
				<u-picker mode="region" v-model="regionShow" @confirm="regionConfirm" :default-region='["北京市", "市辖区", "东城区"]'></u-picker>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="钻孔数目" prop="hole_count">
				<u-input v-model="form.hole_count" type="number" placeholder="请输入共需要钻孔的数目" />
			</u-form-item>
			<!-- <view style="color: #ccc;">请分配孔号，多个用'、'隔开，例如：1235、3、45、2</view>
			<u-form-item label-width="120" required="true" label="钻孔孔号" prop="hole_num">
				<u-input v-model="form.hole_num" placeholder="请输入需要分配的孔号" />
			</u-form-item> -->
			<u-form-item label-width="120" required="true" label="甲方信息" prop="project_jia_info">
				<u-input v-model="form.project_jia_info" placeholder="请输入甲方公司名称" />
			</u-form-item>
			<u-form-item label-width="120" label="结算方式" prop="salary_way">
				<u-input v-model="form.salary_way" placeholder="请选择项目结算方式" type="select" :select-open="salaryWayShow" @click="salaryWayShow = true"/>
				<u-select v-model="salaryWayShow" mode="single-column" @confirm="salaryWayConfirm" :list="salaryWays"></u-select>
			</u-form-item>
			<u-form-item label-width="120" label="结算标准" prop="salary_standard_chi">
				<u-input v-model="form.salary_standard_chi" placeholder="请输入按尺结算的标准"/>
			</u-form-item>
			<u-form-item label-width="120" label="结算标准" prop="salary_standard_day">
				<u-input v-model="form.salary_standard_day" placeholder="请输入按天结算的标准"/>
			</u-form-item>
			<u-form-item label-width="120" required="true" label="项目编号" prop="project_id">
				<view class="u-flex">
					<u-input class="u-flex-1" disabled="true" v-model="form.project_id" placeholder="请点击获取项目编号" />
					<u-button v-if="!form.project_id" class="u-margin-left-20" :disabled="getProjectIDdisable" type="primary" @click="getProjectID" size="mini">点击获取编号</u-button>
				</view>
			</u-form-item>
			<u-form-item label-width="140" :border-bottom="false" label="进场时间">
				<u-input v-model="form.project_begin_date" disabled="true" placeholder="该时间当项目正式启动后系统自动填写"/>
			</u-form-item>
			<u-form-item label-width="140" :border-bottom="false" label="出场时间">
				<u-input v-model="form.project_end_date" disabled="true" placeholder="该时间当项目正式结束后系统自动填写"/>
			</u-form-item>
			<u-form-item label-width="140" :border-bottom="false" label="钻机编号">
				<u-input v-model="form.drill_id" disabled="true" placeholder="请在项目管理里面进行分配钻机"/>
			</u-form-item>
			<u-form-item label-width="140" :border-bottom="false" label="项目员工">
				<u-input disabled="true" placeholder="请在项目管理里面进行员工分配"/>
			</u-form-item>
			<u-form-item label-width="140" :border-bottom="false" label="项目支出">
				<u-input disabled="true" placeholder="请在项目管理里面进行项目支出管理"/>
			</u-form-item>
			<u-form-item label-width="140" label="项目收入">
				<u-input disabled="true" placeholder="请在项目管理里面进行项目收入管理"/>
			</u-form-item>
			<!-- <u-form-item label-width="140" label="打卡信息">
				<u-input disabled="true" placeholder="请在项目管理里面进行项目收入管理"/>
			</u-form-item> -->
		</u-form>
		<u-button v-if="curProjectID === ''" class='u-margin-20' :disabled="submitBtnDis" type="primary" @click="submit">确认创建</u-button>
		<u-button v-else class='u-margin-20' :disabled="submitBtnDis" type="primary" @click="editProject">确认修改</u-button>
		
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import getRandomID from "@/common/getRandomID.js";
	import allotZK from "@/common/allotZK.js";
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				curProjectID:"",
				regionShow:false,
				submitBtnDis:false,
				getProjectIDdisable:false,
				salaryWayShow:false,
				userInfo:{},
				salaryWays:[
					{
						value: 'c1',
						label: '按尺结算'
					},
					{
						value: 't2',
						label: '按天数结算'
					}
				],
				form:{
					pm_name:"",
					pm_post:"",
					pm_id:"",
					pm_phone:"",
					project_name:"",
					project_address:"",
					hole_count:"",
					project_jia_info:"",
					salary_way:"",
					project_id:"",
					project_status:1, //1待启动 ，2进行中，3已结束
					project_begin_date:"",
					project_end_date:"",
					drill_id:"",
					salary_standard_chi:"", //工资标准(按尺)
					salary_standard_day:"", //工资标准(按天)
					worker_ids:[],
					project_expend:[],
					project_income:[],
					signIns:[],// 打卡信息
					drillDeepCount:0,
					signCountDay:0,
					holes:[]
					
				},
				rules: {
					project_name:[
						{
							required:true,
							message:"请输入项目名称",
							trigger:['blur','change']
						}
					],
					project_address:[
						{
							required:true,
							message:"请输入项目地址",
							trigger:['blur','change']
						}
					],
					hole_count:[
						{
							required:true,
							message:"请输入项目钻孔数量",
							trigger:['blur','change']
						}
					],
					project_jia_info:[
						{
							required:true,
							message:"请输入项目甲方公司名称",
							trigger:['blur','change']
						}
					],
					project_id:[
						{
							required:true,
							message:"请点击按钮获取项目编号",
							trigger:['blur','change']
						}
					]
				}
			}
		},
		// 设置校验规则
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(params) {
			this.curProjectID = ""; //初始化时先清空ID
			if(params._id){
				this.curProjectID = params._id;
				this.getProjectData();
			}
			this.getUserInfo();
		},
		computed:{
			...mapState({
				userID:"userID",
				token:"token"
			})
		},
		methods: {
			submit(){
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						
						if(this.form.hole_count >= 1000){
								this.$refs.uTips.show({
									title: '钻孔数目不能超过1000个',
									type: 'warning'
								})
							return false;
						}
						this.form.holes = allotZK(parseInt(this.form.hole_count));
						
						this.submitBtnDis = true;
						this.$loading("正在保存")
						let res = await this.$network({
							api: "main",
							data:{
								action:"projectAdmin/setProjectInfo",
								data:this.form
							}
						});
						uni.hideLoading();
						this.submitBtnDis = false;
						if (res.status === 0) {
							Object.assign(this.$data,this.$options.data())
							this.$refs.uTips.show({
								title: '创建成功',
								type: 'success'
							})
						}
					}
				})
			},
			async getProjectData(){
				this.$loading("请稍后")
				let res = await this.$network({
					api: "main",
					data:{
						action:"projectAdmin/getProjectDetail",
						data:{id:this.curProjectID}
					}
				});
				uni.hideLoading();
				if (res.status === 0) {
					Object.assign(this.form,res.data[0]);
				}
			},
			async editProject(){
				this.$refs.uForm.validate(async valid => {
					if (valid) {
						
						if(this.form.hole_count >= 1000){
								this.$refs.uTips.show({
									title: '钻孔数目不能超过1000个',
									type: 'warning'
								})
							return false;
						}
						this.form.holes = allotZK(parseInt(this.form.hole_count));
						
						delete this.form._id;
						this.submitBtnDis = true;
						this.$loading("正在修改")
						let res = await this.$network({
							api: "main",
							data:{
								action:"projectAdmin/editProject",
								data:{
									token:this.token,
									data:this.form,
									_id:this.curProjectID
								}
							}
						});
						uni.hideLoading();
						this.submitBtnDis = false;
						if (res.status === 0) {
							// uni.navigateBack({
							// 	delta:1
							// })
							this.$refs.uTips.show({
								title: '修改成功',
								type: 'success'
							})
						}
					}
				})
			},
			//选择项目地址
			regionConfirm(e){
				let address = e.city.label == '市辖区' ? `${e.province.label} ${e.area.label}` : `${e.province.label} ${e.city.label} ${e.area.label}`;
				this.form.project_address = address;
			},
			//获取项目编号
			getProjectID() {
				this.$loading("正在获取");
				this.getProjectIDdisable = true;
				// （pj+8位随机数）
				var id = getRandomID();
				this.checkProjectID("di"+id);
			},
			async checkProjectID(id){
				let res = await this.$network({
					api:"main",
					data:{
						action:"disposeData/checkProjectID",
						data:{id}
					}
				});
				
				uni.hideLoading();
				if(res.status === 0){
					this.form.project_id = id;
				} else if(res.status === 1){
					this.getProjectID();
				}
			},
			async getUserInfo(){
				let {userID,token} = this;
				let res = await this.$network({
					api:"main",
					data:{
						action:"user/getUserInfo",
						data:{
							_userID:userID,
							token
						}
					}
				})
				
				if(res.status === 0){
					this.form.pm_name = res.data[0].name;
					this.form.pm_phone = res.data[0].phone;
					this.form.pm_id = res.data[0]._userID;
					this.form.pm_post = res.data[0].userType === 'pm' ? "项目经理" : "普通员工"
				} else {
					this.$refs.uTips.show({
						title: '请先添加个人信息',
						type: 'warning'
					})
				}
			},
			salaryWayConfirm(params){
				this.form.salary_way = params[0].label;
			}
		}
	}
</script>

<style>

</style>
